import MapFeature from "./MapFeature";
import * as events from '../event';
import * as ftypes from '../featureTypes'
import styles from '../styles';
import { mapManager } from "../MapManager";
import { message } from "ant-design-vue";
import BaseMarker from "./baseMarker";
import { Feature } from "ol";
import BaseStyle from "../style/baseStyle";

/**
 * 基地边界
 */
class Base extends MapFeature {

    static BASE__INDEX = 0;

    /**
     * 构造函数
     * @param {Feature} f 
     * @param {import("../../../util/typedef").BaseInfo} valueObject - 基地信息
     */
    constructor(f, valueObject) {
        super(f);
        if (valueObject) {
            this.setValueOjbect(valueObject);
        } else {
            if (f.get("_valueobject") == undefined) {
                this.setValueOjbect({
                    viewColor:'127dff'
                });
            }
        }
        this.setType(ftypes.BASE);
    }

    /**
     * 已有的中心点,哪个能被当前polygon圈中
     * @returns [count(已圈中的中心点并且有边界),<br/>count(已圈中的中心点并且没有边界)]
     */
    static markersInBase(feature) {
        var returnArr = [];
        var hasBoundsArr = new Array();
        var notHasBoundsArr = new Array();
        var features = mapManager.current.layers.markerSource.getFeatures();
        for (var i = 0, j = features.length; i < j; i++) {
            var feature_temp = features[i]
            var latlng = feature_temp.getGeometry().getCoordinates();
            var bb = feature.getGeometry().intersectsCoordinate(latlng);
            if (bb) {
                var markerFeature = new BaseMarker(feature_temp);
                if (markerFeature.hasBounds()) {
                    hasBoundsArr.push(feature_temp);
                } else {
                    notHasBoundsArr.push(feature_temp);
                }
            }
        }
        returnArr[0] = hasBoundsArr;
        returnArr[1] = notHasBoundsArr;
        return returnArr;
    }


    /**
     * 开始圈定基地
     */
    static beginDraw() {
        MapFeature.innerDraw({ ftype: ftypes.BASE, style: BaseStyle.drawBaseStyle() }, (evt) => {
            var featureWrapper = new Base(evt.feature);
            featureWrapper.setType(ftypes.BASE)
            var arr = Base.markersInBase(evt.feature);
            var hasBoundsArr = arr[0]; // count(已圈中的中心点并且有边界)              
            var notHasBoundsArr = arr[1]; //count(已圈中的中心点并且没有边界)
            if (hasBoundsArr.length > 0) {
                message.error("请选中一个未规划的中心点");
                return false;
            } else if (notHasBoundsArr.length != 1) {
                message.error("请选中一个未规划的中心点");
                return false;
            } else {
                var centerPoint = notHasBoundsArr[0];
                var markerWrapper = new BaseMarker(centerPoint);
                var baseBorderWrapper = new Base(evt.feature, markerWrapper.getValueObject());
                baseBorderWrapper.setId("base_" + baseBorderWrapper.getValueObject().id);
                baseBorderWrapper.setStyle(BaseStyle.getBorderStyleStyle(markerWrapper.getValueObject()));


                baseBorderWrapper.getValueObject().viewColor = BaseStyle.lineColor;
                Base.BASE__INDEX++;
                events.fire(events.types.onAddBase, {
                    type: 'create',
                    data: evt.feature
                });
            }
            return true;
        });
    }

    doEdit() {
        //通知事件
        events.fire(events.types.onAddBase, { type: 'update', data: this.feature });

        //编辑feature
        MapFeature.editFeature(this.feature, (evt) => {
            //修改基地边框
            var feature_temp = evt.features.getArray()[0];
            var thisFeature = evt.features.getArray()[0];//当前操作的Feature

            //判断最后操作的坐标点是否在其他的基地范围内
            var result = mapManager.current.layers.baseSource.getFeaturesAtCoordinate(evt.mapBrowserEvent.coordinate);
            if (result.length > 1) {
                return false;
            }

            //判断新的图形是否还包含原来的大头针  
            var markerFeature = mapManager.current.layers.markerSource.getFeatures();
            var containMarker = false;
            for (var i = 0; i < markerFeature.length; i++) {
                var temp = markerFeature[i];
                var baseMarkerWrapper = new BaseMarker(temp);
                var baseBorderWrapper = new Base(thisFeature);
                if (thisFeature.getGeometry().intersectsCoordinate(temp.getGeometry().getCoordinates())) {
                    if (baseBorderWrapper.getValueObject().id == baseMarkerWrapper.getValueObject().id) {
                        containMarker = true;
                    }
                }
            }


            if (!containMarker) {
                message.error("基地边界需要包含中心点");
                return false;
            }

            //发生事件，更新form表单
            events.fire(events.types.onAddBase, {
                type: 'update',
                data: feature_temp
            });

            return true;

        });
    }



    /**
    * 保存成功
    */
    static saveOk(feature) {
        mapManager.getLayers().baseSource.addFeature(feature);
        mapManager.clearDrawSource();
        MapFeature.setBorderDashed(feature, false);
    }

    static getFeatureIdByBaseId(baseId) {
        return "base_" + baseId;
    }
}

export default Base;